﻿using System;
using viForum.Classes;
using viForum.Controllers;

public partial class Admin_Pages_viForum_Forums_AddForum : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string forumidstr = Request.QueryString["ForumId"];
        if (!string.IsNullOrEmpty(forumidstr))
        {
            // edit an already existing forum
            int forumid = 0;
            if (Int32.TryParse(forumidstr, out forumid))
            {
                Forum forum = ForumController.GetForum(forumid);
                Title.Text = forum.Title;
                Description.Text = forum.Description;
                Categories.SelectedValue = forum.CategoryId.ToString();
            }            
        }
        else
        {
            // add a new forum
        }
    }
    
    protected void Save_Click(object sender, EventArgs e)
    {
        string forumidstr = Request.QueryString["ForumId"];
        if (!string.IsNullOrEmpty(forumidstr))
        {
            // edit an already existing forum
            int forumid = 0;
            if (Int32.TryParse(forumidstr, out forumid))
            {
                Forum forum = ForumController.GetForum(forumid);
                forum.Title = Title.Text;
                forum.Description = Description.Text;
                forum.CategoryId = Convert.ToInt32(Categories.SelectedValue);
                ForumController.UpdateForum(forum);
            }
        }
        else
        {
            // add a new forum
            Forum forum = new Forum();
            forum.Title = Title.Text;
            forum.Description = Description.Text;
            forum.CategoryId = Convert.ToInt32(Categories.SelectedValue);
            ForumController.AddForum(forum);
        }
    }
    protected void Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
}
